﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.Background;
using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;
using Windows.Web.Syndication;

using DesdemonaDespairLib;

namespace BackgroundTasks
{
    public sealed class DesdemonaFeedBackgroundTask : IBackgroundTask
    {

        public async void Run( IBackgroundTaskInstance taskInstance )
        {
            BackgroundTaskDeferral deferral = taskInstance.GetDeferral();

            // Download the feed.
            var blogItems = await Utilities.GetDesdemonaBlogItems();

            // Update the live tile with the feed items.
            UpdateTile( blogItems );

            deferral.Complete();
        }

        private static void UpdateTile( List<DesdemonaSyndicationItem> blogItems )
        {
            // Create a tile update manager for the specified syndication feed.
            var updater = TileUpdateManager.CreateTileUpdaterForApplication();
            updater.EnableNotificationQueue( true );
            updater.Clear();

            // Keep track of the number feed items that get tile notifications. 
            int itemCount = 0;

            // Create a tile notification for each feed item.
            foreach( var item in blogItems )
            {
                if( item.Image != null )
                {
                    XmlDocument tileXml = TileUpdateManager.GetTemplateContent( TileTemplateType.TileWide310x150PeekImageAndText01 );
                    PopulateTileXml( tileXml, item );

                    XmlDocument tileSquareXml = TileUpdateManager.GetTemplateContent( TileTemplateType.TileSquare150x150PeekImageAndText04 );
                    PopulateTileXml( tileSquareXml, item );

                    XmlDocument tileLargeXml = TileUpdateManager.GetTemplateContent( TileTemplateType.TileSquare310x310ImageAndText01 );
                    PopulateTileXml( tileLargeXml, item );

                    // Create new tile notifications for the blog item. 
                    updater.Update( new TileNotification( tileLargeXml ) );
                    updater.Update( new TileNotification( tileXml ) );
                    updater.Update( new TileNotification( tileSquareXml ) );

                    // Don't create more than 5 notifications.
                    if( itemCount++ > 5 ) break;
                }
            }
        }

        private static void PopulateTileXml( XmlDocument tileXml, DesdemonaSyndicationItem item )
        {
            var title = item.Title;
            string titleText = title == null ? String.Empty : title;
            tileXml.GetElementsByTagName( textElementName )[0].InnerText = titleText;
            var imageAttributes = tileXml.GetElementsByTagName( "image" )[0].Attributes;
            var srcAttribute = imageAttributes.GetNamedItem( "src" );
            string src = item.Image.src.Replace( "imgmax=800", "imgmax=310" );
            srcAttribute.InnerText = src;

            var altAttribute = tileXml.CreateAttribute( "alt" );
            altAttribute.InnerText = item.Image.alt;
            imageAttributes.SetNamedItem( altAttribute );

            var addImageQueryAttribute = tileXml.CreateAttribute( "addImageQuery" );
            addImageQueryAttribute.InnerText = "true";
            imageAttributes.SetNamedItem( addImageQueryAttribute );
        }

        static string textElementName = "text";
    }
}

